(function(){
    function SetSiteGame(){
        this.init(); 
    }

    SetSiteGame.prototype = {
        constructor:SetSiteGame,
        init:function(){
            this.nodata = '<tr><td  colspan="7">暂无信息</td></tr>';
            this.paramId = location.search.getParam("sitid");

            this.getReportList();

        
             
    
    
        },
        getReportList:function( ){
            var _this = this;
            var siteId =  this.paramId;
            $.ajax({
                type:"get",
                url:"/admin/sitegames?siteid="+siteId,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
    
                        if( data.length )
                        {
                            var strArr = [];
                            data.forEach(function( value, key  ){
                                var gameid  = value.gameid ;
                                var gName = value.name;
                                var siteid = value.siteid;
                                var split = value.split;//是否分票 1分票 0否
                                var rebate = value.rebate;//返点
                                var cType = value.type;//结算类型1 日结 2月结 0不结
                                var isSplit = split==1?"checked":"";
                                var isDisabled = cType>=1?"disabled":"";

                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  >\
                                                <td><input  type="checkbox"  class="selectgame"  '+isSplit+' data-siteid="'+siteid+'" value="'+gameid+'"></td>\
                                                <td>'+gameid+'</td>\
                                                <td  class="gamename">'+gName+'</td>\
                                                <td><input  type="text"   class="rebateopt  rebateopt'+gameid+'" value="'+rebate+'"></td>\
                                                <td><input  type="text" class="jiesuanopt  jiesuanopt'+gameid+'"   value="'+cType+'"   '+isDisabled+'></td>\
                                                <td><a href="javascript:;" class="btn btn-info  editorBtn" data-siteid="'+siteid+'"  data-gameid="'+gameid+'" >保存</a></td>\
                                            </tr>';

                                    strArr.push( html );

                            });

                            $("#unticketList").html( strArr.join(""));
                            // 设置分票
                            $(".selectgame").unbind("click").click(function(){
                                var isChecked = this.checked;
                                _this.siteId = $(this).data('siteid');
                                _this.gameId = $(this).val();
                                if( _this.siteId )//编辑分票
                                {
                                    var splitValue = isChecked?1:0;
                                    _this.editorTicket(splitValue);
                                }
                                else //还没有设置分票 
                                {
                                    _this.addSplitTicket();

                                }
                            });
                            // 返点编辑
                            $(".editorBtn").unbind("click").click(function(){

                                var fdSiteId = $(this).data('siteid');
                                _this.fdGameId = $(this).data('gameid');
                                _this.fdType = $(".jiesuanopt"+_this.fdGameId).val();
                                _this.fdMoney = $(".rebateopt"+_this.fdGameId).val();
                                var sumitLimit = ( _this.fdType>0&&_this.fdMoney>0)|| (_this.fdType==0&&_this.fdMoney==0);
                                if( !sumitLimit)
                                {
                                    $.alert(0,'返点和结算类型填写数字规则： 必须是同时大于0或者同时等于0');
                                    return false;
                                }
                                if( fdSiteId )//编辑返点
                                {
                                    _this.editorFandian();
                                }
                                else //设置返点 
                                {
                                    _this.addFandian();
                                }
                            });

                        }
                        else
                        {
                            $("#unticketList").html(_this.nodata);

                        }

                    }
                    else
                    {
                        $.alert(code, desc);
                    }


                }
            });

        },
        // 添加分票 
        addSplitTicket:function(){
            var _this = this;
            var gameId = this.gameId;
            var siteId = this.paramId;
            $.ajax({
                type:"get",
                url:"/admin/insert_site_game_split?gameid="+gameId+"&siteid="+siteId+"&split=1",
                success:function( info ){
                    _this.getReportList();

                }
            });
        },
        // 编辑分票
        editorTicket:function( isplit ){
            var _this = this;
            var gameId = this.gameId;
            var siteId = this.siteId;
            $.ajax({
                type:"get",
                url:"/admin/update_site_game_split?gameid="+gameId+"&siteid="+siteId+"&split="+isplit,
                success:function( info ){
                    _this.getReportList();
                }
            });


        },
        // 返佣添加
        addFandian:function(){
            var _this = this;
            var gameId = this.fdGameId;
            var siteId = this.paramId;
            var ctype = this.fdType;//返点类型
            var cMoney = this.fdMoney;//返点
            $.ajax({
                type:"get",
                url:"/admin/insert_site_game_rebate?gameid="+gameId+"&siteid="+siteId+"&type="+ctype+"&money=0"+cMoney,
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code==1 )
                    {
                        $.alert(code,'保存成功!');
                      _this.getReportList();

                    }
                    else
                    {
                        $.alert(code,desc);
                    }

                }
            });

        },
        // 返佣编辑
        editorFandian:function(){
            var _this = this;
            var gameId = this.fdGameId;
            var siteId = this.paramId;
            var ctype = this.fdType;//结算类型类型
            var cMoney = this.fdMoney;//返点
            $.ajax({
                type:"get",
                url:"/admin/update_site_game_rebate?gameid="+gameId+"&siteid="+siteId+"&type="+ctype+"&money=0"+cMoney,
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code==1 )
                    {
                        $.alert(code,'保存成功!');
                       _this.getReportList();
                    }
                    else
                    {
                        $.alert(code,desc);
                    }

                }
            });

        },

 
   
 
      
    };





  new SetSiteGame();

}());